package NewTmas.TableModels;

import taskmaster.job.Task;
import taskmaster.job.TaskCollection;
import taskmaster.util.Global;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/6/11
 * Time: 9:48 AM
 * To create a table Model for the Task collection Table
 */
public class TasksTableModel extends AbstractTableModel implements TableModelListener {
   public TaskCollection taskCollection;

    String[]   columnNames;

    Object[][] data;


    public TasksTableModel(TaskCollection taskCollection) {
        this.taskCollection=taskCollection;
        populateTable(taskCollection);

    }

    public void populateTable(TaskCollection taskCollection) {
        columnNames=new String[]{"Job","Task","Status","Description","Percent Completed","Host","Elapsed Time","Start Time","End Time","Source","Command","Waits Until"};
        Task[] tobeDisplayedTasks=taskCollection.getAllTasksArray();
        data =new Object[tobeDisplayedTasks.length][columnNames.length];
        SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");

        for(int i=0;i<tobeDisplayedTasks.length;i++) {

            data[i][0]=tobeDisplayedTasks[i].getJobTag() ;
            data[i][1]=tobeDisplayedTasks[i].getTaskTag();
            data[i][2]= Global.statusNames.get(tobeDisplayedTasks[i].getStatusId());
            data[i][3] =tobeDisplayedTasks[i].getDescription();
            if(tobeDisplayedTasks[i].isRunning())
            {
                data[i][4]=String.valueOf((int)tobeDisplayedTasks[i].getPercentFinishedVsLast())+"%" ;
                data[i][5]=tobeDisplayedTasks[i].getHost() ;
            }
            else
            {
                data[i][4]="";
                data[i][5]="" ;
            }
            data[i][6]=tobeDisplayedTasks[i].getFormattedElapsedTime() ;
            data[i][7]=tobeDisplayedTasks[i].getStartTime();
            data[i][8]=tobeDisplayedTasks[i].getEndTime();
            data[i][9]=tobeDisplayedTasks[i].getSource();
            data[i] [10]=tobeDisplayedTasks[i].getCommand();
            if(tobeDisplayedTasks[i].getCronStartTime()!=null){
                Date d = new Date(tobeDisplayedTasks[i].getCronStartTime().getTime());
                data[i][11] =timeFormat.format(d);
            } else
            {
                data[i][11]="";

            }
        }
    }


    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }


    public String getColumnName(int col) {
        return columnNames[col];
    }

    public void tableChanged(TableModelEvent e) {
        fireTableChanged(e);
    }
}
